<?php

if (!defined('IN_ADMIN')) {
    exit('Access Denied');
}

$mpurl = 'admin.php?ac=user_profile';
$list = array();
if ($_GET['op'] == "modifyUserPasswd") {
    $uid = intval($_GET ['uid']);
    $param = array(
        'do' => 'modifyUserPasswd',
        'format' => 'json',
        'uid' => $_GET['uid'],
        'passwd' => md5($_GET['passwd']),
    );

    $content = get_api_content($_SC ['apiurl'], $param);
    $content = json_decode($content);

    $resetResult = '';
    if ($content->resultData == 1) {

        $resetResult = '操作成功';
        $msg = "操作成功!";
        cpmessage($msg);
    }
    $msg = "操作失败，请重试一次!";
    if ($content->errorNo) {
        $resetResult = '操作失败，请重试一次';
        $msg .= "errorNo:" . $content->errorNo;
    }
    cpmessage($msg);
}elseif ($_GET['op'] == "regainCloudSync"){
    $uid = intval($_GET ['uid']);
    $param = array(
            'do' => 'regainCloudSync',
            'format' => 'json',
            'uid' => $uid,
    );
    
    $content = get_api_content($_SC ['apiurl'], $param);
    $content = json_decode($content);
    
    $resetResult = '';
    if ($content->resultData == 1) {
    
        $resetResult = '操作成功';
    
        $msg = "操作成功!";
        cpmessage($msg);
    }
    $msg = "操作失败，请重试一次!";
    if ($content->errorNo) {
        $resetResult = '操作失败，请重试一次';

        $msg .= "errorNo:" . $content->errorNo;
    }
    cpmessage($msg);
}elseif ($_GET['op'] == "cloudRemoveByUid"){
    $uid = intval($_GET ['uid']);
    $param = array(
            'do' => 'cloudRemoveByUid',
            'format' => 'json',
            'uid' => $uid,
    );
    
    $content = get_api_content($_SC ['apiurl'], $param);
    $content = json_decode($content);
    
    $resetResult = '';
    if ($content->resultData == 1) {
    
        $resetResult = '操作成功';
    
        $msg = "操作成功!";
        cpmessage($msg);
    }
    $msg = "操作失败，请重试一次!";
    if ($content->errorNo) {
        $resetResult = '操作失败，请重试一次';
    
        $msg .= "errorNo:" . $content->errorNo;
    }
    cpmessage($msg);
}elseif($_GET['op'] == "delAllDiary"){
    $uid   = intval($_GET ['uid']);
    $param = array(
            'do'    => 'delAllDiary',
            'format'=> 'json',
            'uid'   =>  $uid
    );
    
    $content = get_api_content($_SC ['apiurl'], $param);
    $content = json_decode($content);
    
    $resetResult = '操作失败，请重试一次!';
    if ($content->resultData) {
        $resetResult = '操作成功';
        $msg = "操作成功!";
        header('Location: '.'admin.php?ac=user_profile&uid='.$uid);
    }
    if ($content->errorNo) {
        $resetResult = '操作失败，请重试一次';
        $msg .= "errorNo:" . $content->errorNo;
    }
    cpmessage($msg);
    
}elseif($_GET['op'] == "removeAllComment"){
    $uid   = intval($_GET ['uid']);
    $param = array(
            'do'    => 'removeAllComment',
            'format'=> 'json',
            'uid'   =>  $uid
    );
    
    $content = get_api_content($_SC ['apiurl'], $param);
    $content = json_decode($content);
    
    $resetResult = '操作失败，请重试一次!';
    if ($content->resultData) {
        $resetResult = '操作成功';
        $msg = "操作成功!";
        header('Location: '.'admin.php?ac=user_profile&uid='.$uid);
    }
    if ($content->errorNo) {
        $resetResult = '操作失败，请重试一次';
        $msg .= "errorNo:" . $content->errorNo;
    }
    cpmessage($msg);
    
}elseif($_GET['op'] == "restoreAllDiary"){
    $uid   = intval($_GET ['uid']);
    $param = array(
            'do'    => 'restoreAllDiary',
            'format'=> 'json',
            'uid'   =>  $uid
    );
    
    $content = get_api_content($_SC ['apiurl'], $param);
    $content = json_decode($content);
    
    $resetResult = '操作失败，请重试一次!';
    if ($content->resultData) {
        $resetResult = '操作成功';
        $msg = "操作成功!";
        header('Location: '.'admin.php?ac=user_profile&uid='.$uid);
    }
    if ($content->errorNo) {
        $resetResult = '操作失败，请重试一次';
        $msg .= "errorNo:" . $content->errorNo;
    }
    cpmessage($msg);
    
}elseif ($_GET['op'] == 'modifyManyUsersExp'){
    $exp      = intval($_POST ['exp']);
    $uidsStr  = strval($_POST['uidsStr']);
    $uids     = explode(",", $uidsStr);
    
    if ($exp < 1 || count($uids) < 1 ){
        $msg = "参数非法!";
        cpmessage($msg);
        return FALSE;
    }
    
    $num = 0;
    foreach ($uids as $value){
        $param = array(
                'do'       => 'changeExpLevel',
                'format'   => 'json',
                'uid'      => $value,
                'exp'	   => $exp
        );
    
        $content = get_api_content($_SC ['apiurl'], $param);
        $content = json_decode($content);
        
        if ($content->resultData == 1){
            // 发通知
            if ($_POST['content']){
                sendMessage(1, $value, $_POST['content']);
            }
            $num ++;
        }
    }
    if ($num > 0){
        $msg = "修改成功，共有<".$num.">个用户!";
    }else{
        $msg = "修改失败!";
    }
    
    cpmessage($msg);
}elseif ($_GET['op'] == "modifyUserLevelExp"){
    $uid   = intval($_POST ['uid']);
    $exp   = intval($_POST ['exp']);
    $param = array(
            'do'    => 'changeExpLevel',
            'format'=> 'json',
            'uid'   =>  $uid,
    		'exp'	=>	$exp
    );
    
    $content = get_api_content($_SC ['apiurl'], $param);
    $content = json_decode($content);
    
    $resetResult = '操作失败，请重试一次!';
    if ($content->resultData == 1) {
        // 发通知
        if ($_POST['content']){
            sendMessage(1, $uid, $_POST['content']);
        }
        
        $resetResult = '操作成功';
        $msg = "操作成功!";
        header('Location: '.'admin.php?ac=user_profile&uid='.$uid);
        cpmessage($msg,$url);
    }
    if ($content->errorNo) {
        $resetResult = '操作失败，请重试一次';
        $msg .= "errorNo:" . $content->errorNo;
    }
    cpmessage($msg);
}elseif ($_GET['op'] == "changeUserCoin"){
    $uid   = intval($_GET ['uid']);
    $coin   = intval($_GET ['coin']);
    $param = array(
            'do'    => 'doTask',
            'format'=> 'json',
            'mUid'   =>  $uid,
            'uid'   =>  $uid,
    		'coin'	=>	$coin,
            'task' => 'system_award'
    );

    $content = get_api_content($_SC ['apiurl'], $param);
    $content = json_decode($content);
    
    $resetResult = '操作失败，请重试一次!';
    if ($content->resultData == 1) {
        $resetResult = '操作成功';
        $msg = "操作成功!";
        header('Location: '.'admin.php?ac=user_profile&uid='.$uid);
    }
    if ($content->errorNo) {
        $resetResult = '操作失败，请重试一次';
        $msg .= "errorNo:" . $content->errorNo;
    }
    cpmessage($msg);
} elseif($_GET['op'] == "jinyanmsg"){
	$msg = $_GET['msg'];
	$uid = $_GET['uid'];
	addShutUpMsg($uid,$msg);
	header("Content-type: application/json");
	echo 1;
	exit;
} else{
    $status  = 0;
    $uid     = intval($_GET ['uid']);
    isset($_GET['status'])    &&    $status    =    intval($_GET['status']);
    
    $param = array(
        'do'     => 'getUserInfo',
        'format' => 'json',
        'uid'    => $uid,
        'status' => $status,
    );

    $userData = get_api_content($_SC ['apiurl'], $param);
    $userData = json_decode($userData);
    $userData = object_to_array($userData->resultData);
    
    // 统计精华话题数
    $tmp = array(
            'do'    =>    'countUserDigestTopicNum',
            'format'=>    'json',
            'uid'   =>    $uid
    );
    $content = get_api_content($_SC ['apiurl'], $tmp);
    $content = json_decode($content);
    $total   = object_to_array($content->resultData);
    $total   = $total['num'];
    $jybz = getShutUpMsg($uid);
}

function sendMessage($uid, $mUid, $content){
	global $_SC;
    $param = array(
            'do'       => 'adminSendMessage',
            'format'   => 'json',
            'uid'      => $uid,
            'mUid'     => $mUid,
            'content'  => getstr($content, 512),
    );
    
    $content = get_api_content($_SC ['apiurl'], $param);
    $content = json_decode($content);
    $result = object_to_array($content->resultData);
    return $result;
}
function addShutUpMsg($uid,$msg){
	$file = '/tmp/forbiddenInfo/shutUpMsg.txt';
	$b = array();
	$a = file_get_contents($file);
	if($a){
		$b = json_decode($a,true);
	}
	$b[$uid] = $msg;
	$c = json_encode($b);
	file_put_contents($file,$c);
}
function getShutUpMsg($uid){
	$file = '/tmp/forbiddenInfo/shutUpMsg.txt';
	$b = array();
	$a = file_get_contents($file);
	if($a){
		$b = json_decode($a,true);
	}
	return $b[$uid];
}

